package com.czy.owner.ui.wheel;

import android.content.Intent;
import android.support.design.widget.AppBarLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.entity.SpecialGoodsAttrModel;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.MyLog;
import com.weigan.loopview.LoopView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ChooseWheelSpecActivity extends BaseActivity {

    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_subtitle)
    TextView toolbarSubtitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.loop_view_tmk)
    LoopView loopViewTmk;//胎面宽LoopView
    @BindView(R.id.loop_view_code_xg)
    LoopView loopViewCodeXG;//斜杠
    @BindView(R.id.loop_view_bpb)
    LoopView loopViewBpb;//扁平比
    @BindView(R.id.loop_view_code_r)
    LoopView loopViewCodeR;//R字母
    @BindView(R.id.loop_view_zj)
    LoopView loopViewZJ;//直径
    @BindView(R.id.tv_search_ensure)
    TextView tvSearchEnsure;

    private List<SpecialGoodsAttrModel.AttributesListBean> attributesList;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_choose_wheel_spec;
    }

    @Override
    protected void initInjector() {

    }

    /**
     * type 在列表中的位置
     * */
    int tmkIndex = 0;
    int bpbIndex = 0;
    int zjIndex = 0;

    /**
     * 类型的选中值所在位置
     * */
    int selectedTMKIndex = 0;
    int selectedBPBIndex = 0;
    int selectedZJIndex = 0;

    @Override
    protected void initViews() {
        toolbarTitle.setText("选择规格");
        //获取源数据
        attributesList = (List<SpecialGoodsAttrModel.AttributesListBean>) getIntent().getSerializableExtra("attributes_list");
        //获取初始状态值
        selectedTMKIndex = getIntent().getIntExtra("selected_tmk_index", 0);
        selectedBPBIndex = getIntent().getIntExtra("selected_bpb_index", 0);
        selectedZJIndex = getIntent().getIntExtra("selected_zj_index", 0);
        MyLog.d("RAVEN","init tmkIndex = " + selectedTMKIndex);
        MyLog.d("RAVEN","init  bpbIndex = " + selectedBPBIndex);
        MyLog.d("RAVEN","init  zjIndex = " + selectedZJIndex);
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("/");

        ArrayList<String> list4 = new ArrayList<>();
        list4.add("R");

        if (attributesList != null) {
            for (int i = 0; i < attributesList.size(); ++i) {
                if (attributesList.get(i).getGoodsAttributesName().equals("胎面宽")) {
                    if(attributesList.get(i).getValues()!=null && attributesList.get(i).getValues().size()>0) {
                        initWheelViews(loopViewTmk, attrValueToStr(attributesList.get(i).getValues()), false);
                        tmkIndex = i;
                        loopViewTmk.setInitPosition(selectedTMKIndex);
                        loopViewTmk.setVisibility(View.VISIBLE);
                    }else{
                        loopViewTmk.setVisibility(View.INVISIBLE);
                    }
                } else if (attributesList.get(i).getGoodsAttributesName().equals("扁平比")) {
                    if(attributesList.get(i).getValues()!=null && attributesList.get(i).getValues().size()>0) {
                        bpbIndex = i;
                        initWheelViews(loopViewBpb, attrValueToStr(attributesList.get(i).getValues()), false);
                        loopViewBpb.setInitPosition(selectedBPBIndex);
                        loopViewBpb.setVisibility(View.VISIBLE);
                    }else{
                        loopViewBpb.setVisibility(View.INVISIBLE);
                    }
                } else if (attributesList.get(i).getGoodsAttributesName().equals("直径")) {
                    if(attributesList.get(i).getValues()!=null && attributesList.get(i).getValues().size()>0) {
                        zjIndex = i;
                        initWheelViews(loopViewZJ, attrValueToStr(attributesList.get(i).getValues()), false);
                        loopViewZJ.setInitPosition(selectedZJIndex);
                        loopViewZJ.setVisibility(View.VISIBLE);
                    }else {
                        loopViewZJ.setVisibility(View.INVISIBLE);
                    }
                }
            }

            initWheelViews(loopViewCodeXG, list2, false);
            initWheelViews(loopViewCodeR, list4, false);
        }

        tvSearchEnsure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("selected_tmk_index", loopViewTmk.getSelectedItem());
                intent.putExtra("selected_bpb_index", loopViewBpb.getSelectedItem());
                intent.putExtra("selected_zj_index",  loopViewZJ.getSelectedItem());
                ChooseWheelSpecActivity.this.setResult(RESULT_OK,intent);
                ChooseWheelSpecActivity.this.finish();
            }
        });


    }

    private List<String> attrValueToStr(List<SpecialGoodsAttrModel.AttributesListBean.ValuesBean> valuesBeanList) {
        List<String> datas = new ArrayList<>();
        for (SpecialGoodsAttrModel.AttributesListBean.ValuesBean valuesBean : valuesBeanList) {
            datas.add(valuesBean.getAttributesValue());
        }
        return datas;
    }

    @Override
    protected void updateViews() {

    }

    private void initWheelViews(LoopView loopView, List<String> list, boolean loop) {
        loopView.setCenterTextColor(ContextCompat.getColor(this, R.color.appThemeColor));
        loopView.setOuterTextColor(ContextCompat.getColor(this, R.color.appThemeSub2TitleColor));
        loopView.setTextSize(14);
        loopView.setDividerColor(ContextCompat.getColor(this, R.color.appThemeBorderColor));
        if (!loop) {
            loopView.setNotLoop();
        }
        //设置原始数据
        loopView.setItems(list);

        //设置初始位置
        loopView.setInitPosition(0);
    }

}
